package com.company.cloud.common.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSON;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;


public class MainUtils {
	
	private static String propertiesFileName="application.properties";
	
	public static String[] args(String[] args) {
		String PropertiesFilePath;
		String os = System.getProperty("os.name");
		if (os.toLowerCase().startsWith("win")) { //windows
			PropertiesFilePath=StrUtil.format("{}/{}", "C://opt/data",propertiesFileName);
       }else if(os.toLowerCase().startsWith("mac")) { //mac
    	   PropertiesFilePath=StrUtil.format("{}/{}", "/opt/data",propertiesFileName);
       }else {
    	   PropertiesFilePath=StrUtil.format("{}/{}", "/opt/data",propertiesFileName);
       }
		System.out.println(PropertiesFilePath);
		return args(args, "--", PropertiesFilePath);
	}
	
	public static String[] args(String[] args,String propPrefix, Set<String>  propFilePaths) {
		Set<String> propertyList=null;
		try {
			propertyList=PropertiesUtils.readListByFile(propPrefix,propFilePaths);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("++++++加载默认配置错误");
		}
		return args(args, propertyList);
	}
	
	public static String[] args(String[] args,String propPrefix, String propFilePath) {
		Set<String> propertyList=null;
		try {
			propertyList=PropertiesUtils.readListByFile(propPrefix,propFilePath);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("++++++加载默认配置错误");
		}
		return args(args, propertyList);
	}
	
	private static String[] args(String[] args,Set<String> propertyList) {
		List<String> argList;
		if(ArrayUtil.isNotEmpty(args)) {
			argList=CollUtil.newArrayList(args);
		}else {
			argList=new ArrayList<String>();
		}
		if(propertyList==null) {
			propertyList=new HashSet<String>();
		}
		Collection<String> unionList=CollUtil.union(argList, propertyList);
		String[] args2 = new String[unionList.size()];
		unionList.toArray(args2);
		return args2;
	}
	
	public static void main(String[] args) {
		String[] sss=new String[] {"--nacos.namespace=saas-2","2222"};
		String[] s=MainUtils.args(sss);
		System.out.println(JSON.toJSONString(s));
		
		

	}

}
